package cn.laixueit.controller;

import cn.laixueit.exception.StudentException;
import cn.laixueit.pojo.ClassInfo;
import cn.laixueit.pojo.Student;
import cn.laixueit.service.ClassService;
import cn.laixueit.service.StudentService;
import cn.laixueit.utils.Utils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/student")
@SessionAttributes("classList")
@Scope("prototype")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;

    @RequestMapping("/findall")
    public String findall(@RequestParam(defaultValue = "1") Integer startpage, @RequestParam(defaultValue = "10") Integer pagesize, @ModelAttribute Student student, Model model) {
        PageInfo<Student> pageInfo = studentService.findall(startpage, pagesize, student);
        model.addAttribute("pageinfo", pageInfo);
        List<ClassInfo> classInfoList = classService.selectall();
        model.addAttribute("classList",classInfoList);
        return "studentList";
    }

    @GetMapping("/del/{id}")
    public String deleteById(@PathVariable("id") Integer id) {
        studentService.deleteById(id);
        return "redirect:/student/findall";
    }

    @PostMapping("/save")
    public String save(Student student, String birthday) {
        studentService.save(student, birthday);
        return "redirect:/student/findall";
    }

    @GetMapping("/editbyid/{id}")
    public String editbyid(@PathVariable Integer id, Model model) {
        Student student = studentService.selectById(id);
        model.addAttribute("student", student);
        return "studentEdit";
    }

    @PostMapping("/update")
    public String update(MultipartFile img, Student student, HttpServletRequest request) throws StudentException {
        if(img.getSize()>0) {
            //
            String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            String path = request.getSession().getServletContext().getRealPath("/img/" + date + "/");
            File imgDir = new File(path);
            if (!imgDir.exists()) {
                imgDir.mkdirs();
            }
            String imgName = img.getOriginalFilename();
            imgName = Utils.getUUID(8) + imgName;
            System.out.println(img.getName() + img.getContentType());

            student.setImgurl("/img/" + date + "/" + imgName);
            try {
                img.transferTo(new File(imgDir, imgName));
            } catch (Exception e) {

            }
        }
        studentService.update(student);
        return "redirect:/student/findall";
    }

}
